//
//  ZQEmotionToolBar.swift
//  ZQWeibo
//
//  Created by summer的Dad on 16/11/23.
//  Copyright © 2016年 summer的Dad. All rights reserved.
//

import UIKit

enum EmotionType : Int{
    case RECENT = 0
    case DEFAULT
    case EMOJI
    case LXH
    
}



class ZQEmotionToolBar: UIStackView {

    var lastBtn : UIButton = UIButton()
    
     var enmotionBtnTag:((EmotionType) -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.orange
        
        setuoUI()
        //设置轴和填充方式
        axis = .horizontal
        
        distribution = .fillEqually
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func setuoUI(){
        //最近
        addClickButton(title: "最近", picimage: "compose_emotion_table_left",index :.RECENT )
        //默认
        addClickButton(title: "默认", picimage: "compose_emotion_table_mid",index : .DEFAULT)
        //emoji
         addClickButton(title: "emotion", picimage: "compose_emotion_table_mid",index : .EMOJI)
        //浪小花
         addClickButton(title: "浪小花", picimage: "compose_emotion_table_right",index : .LXH)
        
        
        
        
    }
    
    
    func addClickButton(title: String , picimage : String,index : EmotionType ){
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named:picimage + "_normal"), for: .normal)
        btn.setBackgroundImage(UIImage(named:picimage + "_selected"), for: .selected)
        btn.tag = index.rawValue
        btn.setTitle(title, for: .normal)
        //
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .selected)
        //添加点击事件
        btn.addTarget(self, action:#selector(clickbtnincident(btn:)), for: .touchUpInside)
        
       //添加button到视图
        self.addArrangedSubview(btn)
        //判断tag
        if index == .RECENT {
            
           btn.isSelected = true
           //记录上一次的BTn
           lastBtn = btn
        }
        
    }
    //点击事件
    func clickbtnincident(btn:UIButton){
        
        
         lastBtn.isSelected = false
        //赋值
        lastBtn = btn
        
        lastBtn.isSelected = true
        //实行闭包
        enmotionBtnTag?(EmotionType.init(rawValue: btn.tag)!)
   
    }

}
